<script setup>
import { useRouter } from "vue-router";
const router = useRouter();
import { UseMenuStore } from "../store/index";
const usmenustore = UseMenuStore();
const props = defineProps(["menuData", "index"]);

// 点击左侧栏菜单
const handleClick = (item, active) => {
  // console.log(item.meta.path);
  // 存储当前tab栏，用于后面页面刷新不丢失
  usmenustore.RouterPath = item.meta.path;
  router.push(item.meta.path);
  usmenustore.addMenu(item.meta);
  usmenustore.MenuActive = active;
};
</script>

<template>
  <template v-for="(item, index) in props.menuData">
    <el-menu-item
      @click="handleClick(item, `${props.index}-${item.meta.id}`)"
      :index="`${props.index}-${item.meta.id}`"
      v-if="!item.children || item.children.length === 0"
      :key="`${props.index}-${item.meta.id}`"
    >
      <el-icon size="20">
        <component :is="item.meta.icon"></component>
      </el-icon>
      <span>{{ item.meta.name }}</span>
    </el-menu-item>

    <el-sub-menu :index="`${props.index}-${item.meta.id}`" v-else>
      <!-- 这是标题栏，点击展开子层的 -->
      <template #title>
        <el-icon size="20">
          <component :is="item.meta.icon"></component>
          <!-- <el-icon><Plus /></el-icon> -->
        </el-icon>
        <span>{{ item.meta.name }}</span>
      </template>

      <!-- 递归渲染子层 -->
      <tree-menu
        :index="`${props.index}-${item.meta.id}`"
        :menuData="item.children"
      />
    </el-sub-menu>
  </template>
</template>

<style scoped lang="less"></style>
